package com.ithuameng.notice.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ithuameng.notice.util.R;
import com.ithuameng.notice.netty.MyWebSocketHandler;
import com.rabbitmq.client.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;

import java.util.HashMap;

/**
 * 配置mq监听器
 */
public class SysNoticeListener implements ChannelAwareMessageListener {

    private static ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        io.netty.channel.Channel wsChannel = MyWebSocketHandler.userChannelMap.get("admin");
        // 检查超级管理员是否在线
        if (wsChannel != null) {
            HashMap<String, Integer> countMap = new HashMap<>();
            countMap.put("sysNoticeCount", 1);
            R result = new R<>(true, 200, "查询成功", countMap);
            wsChannel.writeAndFlush(new TextWebSocketFrame(MAPPER.writeValueAsString(result)));
        }
    }
}